﻿using habbo.Habbo.Items;
using habbo.Kernel.Packets.Messages;
using habbo.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace habbo.Habbo.Catalog
{
    class CatalogItem
    {
        /// <summary>
        /// Id of the page
        /// </summary>
        public int Id;

        /// <summary>
        /// Item ids
        /// </summary>
        public List<int> ItemIds;

        /// <summary>
        /// Name of the item
        /// </summary>
        public string Name;

        /// <summary>
        /// How much does this item cost?
        /// </summary>
        public int CreditsCost;

        /// <summary>
        /// Extra Currencies?
        /// </summary>
        public int ExtraCost;

        /// <summary>
        /// Amount of things to be sold
        /// </summary>
        public int Amount;

        public bool IsDeal
        {
            get
            {
                if (ItemIds.Count > 1)
                {
                    return true;
                }

                return false;
            }
        }

        public CatalogItem(int Id, string Name, string ItemIds, int CreditsCost, int ExtraCost, int Amount)
        {
            this.Id = Id;
            this.Name = Name;
            this.ItemIds = new List<int>();

            foreach (string ItemId in ItemIds.Split(','))
            {
                this.ItemIds.Add(int.Parse(ItemId));
            }

            this.CreditsCost = CreditsCost;
            this.ExtraCost = ExtraCost;
            this.Amount = Amount;
        }

        public Item GetBaseItem()
        {
            if (IsDeal)
                return null;

            return SystemApp.HabboSystem.ItemManager.GetItem(ItemIds[0]);
        }

        public Item GetBaseItemById(int ItemId)
        {
            if (IsDeal)
                return null;

            return SystemApp.HabboSystem.ItemManager.GetItem(ItemId);
        }

        public void Serialize(ServerPacket Packet, CatalogPage Page)
        {
            if (IsDeal)
                throw new NotImplementedException("Multipile item ids set for catalog item #" + Id + ", but this is usupported at this point");
            else
            {
                Packet.Append(GetBaseItem().Id);
                Packet.Append(GetBaseItem().PublicName);
                Packet.Append(CreditsCost);
                Packet.Append(0); //cba, pixels, extracostings?
                Packet.Append(0);
                Packet.Append(GetBaseItem().AllowGift);
                Packet.Append(1);

                Packet.Append(GetBaseItem().Type);
                Packet.Append(GetBaseItem().SpriteId);

                if (Page.Layout == "spaces")
                {
                    if (GetBaseItem().Name.Contains("wallpaper") || GetBaseItem().Name.Contains("floor") || GetBaseItem().Name.Contains("landscape"))
                    {
                        Packet.Append(GetBaseItem().Name.Split('_')[2]);
                    }
                }
                else
                    Packet.Append("");

                Packet.Append(Amount);
                Packet.Append(-1);
                Packet.Append(false);

                Packet.Append(0); //isVIP ? 1 : 0
                Packet.Append(false);
            }
        }
    }
}
